package com.jacky.concurrent;

import java.util.concurrent.Semaphore;

/**
 * Created by jacky on 2018/2/12.
 */
public class SemaphoreDemo {
    public static void main(String[] args) {
        //Semaphore semaphore = new Semaphore(2, true);
        MySemaphore semaphore = new MySemaphore(2, true);
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                Thread thread = Thread.currentThread();
                System.out.println("semaphore start:"+thread.getName());
                try {
                    semaphore.acquire();
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                semaphore.release();
                System.out.println("semaphore end:"+thread.getName());

            }
        };

        for (int i=0;i<10;i++){
            Thread thread = new Thread(runnable, "t" + i);
            thread.start();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
